--[rpt_AI_TBBP_YTD] '2009-12-30','C20000'
create proc [dbo].[rpt_AI_TBBP_YTD]
@ToDate datetime,
@CardCode nvarchar(max)
as
declare @FromDate datetime
set @FromDate=convert(nvarchar(4),YEAR(@todate)) + '-01-01'

declare @tmp table(MonthYear int)
declare @i int
set @i=1
while @i<=MONTH(@ToDate)
begin
	insert into @tmp values (@i)
	set @i=@i+1
end


select MonthYear, SUM(debit-credit) Balance 
from @tmp T0
left join JDT1 T1 on T0.MonthYear=MONTH(RefDate) and ShortName=@CardCode and RefDate between @FromDate and @ToDate
group by MonthYear
union all
select 0, SUM(debit-credit) Balance 
from JDT1 where RefDate<@FromDate and ShortName=@CardCode